<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="row justify-content-center">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h4 class="mb-0">用户注册</h4>
            </div>
            <div class="card-body">
                <div id="errorAlert" class="alert alert-danger d-none" role="alert"></div>
                <form id="registerForm">
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名</label>
                        <input type="text" class="form-control" id="username" name="username" required="">
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">密码</label>
                        <input type="password" class="form-control" id="password" name="password" required="">
                    </div>
                    <div class="mb-3">
                        <label for="confirmPassword" class="form-label">确认密码</label>
                        <input type="password" class="form-control" id="confirmPassword" name="confirmPassword" required="">
                    </div>
                    <div class="mb-3">
                        <label for="email" class="form-label">邮箱</label>
                        <input type="email" class="form-control" id="email" name="email">
                    </div>
                    <button type="submit" class="btn btn-primary">注册</button>
                    <a href="/quiz_system/user/login" class="btn btn-link">已有账号？立即登录</a>
                </form>
            </div>
        </div>
    </div>
</div>

<script>

    $(document).ready(function() {
        $('#registerForm').on('submit', function(e) {
            e.preventDefault();
            console.log('Form submitted');

            // 表单验证
            if ($('#password').val().length < 6) {
                $('#errorAlert').removeClass('d-none').text('密码长度不能少于6位');
                return;
            }

            if ($('#password').val() !== $('#confirmPassword').val()) {
                $('#errorAlert').removeClass('d-none').text('两次输入的密码不一致');
                return;
            }

            var userData = {
                username: $('#username').val(),
                password: $('#password').val(),
                email: $('#email').val()
            };

            console.log('Sending data:', userData);

            $.ajax({
                url: '/quiz_system/user/register',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(userData),
                success: function(response) {
                    console.log('Response:', response);
                    if (response.code === 200) {
                        alert('注册成功，请登录');
                        window.location.href = '/quiz_system/user/login';
                    } else {
                        $('#errorAlert').removeClass('d-none').text(response.message);
                    }
                },
                error: function(xhr, status, error) {
                    console.log('Error:', error);
                    $('#errorAlert').removeClass('d-none').text('注册失败，请稍后重试');
                }
            });
        });
    });

</script>
